public class MainMemory
{
	private MemoryManager manager;
	private PageFrame frames[] = new PageFrame[150];
	
	public MainMemory(MemoryManager m) {
		manager = m;
		setFrames();
	}
	
	public PageFrame[] getFrames() {
		return frames[];
	}
	
	public void loadJob(Job j) { 
		int jobSize = j.getSize();
		int bestFit = 0;
		int currentSize = 0;
		int initSize = 9999;
		int counter = 0;
		
		for(int i=0; i<frames.length; i++)
		{
			if(jobSize > frames[i].getSize())
			{
				counter++;
			}
			else
			{
				currentSize = frame[i].getSize() - jobSize;
			}
			if(currentSize < initSize)
			{
				bestFit = counter;
				initSize = currentSize;
				counter++;
			}
		}
	}
	
	public void setFrames() {
		for(int i=0; i<150; i++)
			frames[i] = new PageFrame(i);
	}
	
	public void setOSSpace(Job os) {
		for(int i=0; i<os.getPCB().getPages().length; i++) {
			frames[i].setPage(os.getPages()[i]);
			os.getPages()[i].setPageFrame(frames[i]);
			os.getPages()[i].changeInMemory();
		}
	}
	
	public int[] findEmptyFrames(int size) {
		int result[] = new int[size];
		int resultIndex = 0;
		int count = 25;
		while(count < 150 && resultIndex <size)
			if(frames[count].isFree()) {
				result[resultIndex] = count;
				resultIndex++;
				count++;
		}
	}
	
	public Page getPageInFrame(int f) throws Exception {
		if(f >= 150)
			throw new PageFrameDoesNotExistException();
		if(frames[f].isFree())
			throw new Exception("There is no page in frame "+f);
		return frames[f].getPage();
	}
	
	public void freeFrames(int index) {
		frames[index].deallocate();
	}
}